<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="规则名称">
              <a-input placeholder="请输入规章名称" v-model="queryParam.ruleName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="生效区域">
              <j-select-depart placeholder="请选择有效区域id" v-model="queryParam.areaId"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="状态">
<!--                <a-input placeholder="请输入状态" v-model="queryParam.status"></a-input>-->
                <j-dict-select-tag type="list" v-model="queryParam.status" dictCode="ps_rule_status" placeholder="请选择状态" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin: 0 auto 10px">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin: 0 0 10px 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text,record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" :preview="record.id" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id ,ruleList)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <span slot="ruleStatus" slot-scope="text, record">
<!--          <j-dict-select-tag type="radioButton" v-model="record.status" dictCode="ps_rule_status" placeholder="请选择状态" />-->
            <a-switch :defaultChecked="record.status == 0 ? true : false" @change="(checked,event) => {selectorSwitch(record,checked,event)}" />
            <span style="margin-left: 10px">{{record.status == 0 ? '启用' : '停用'}}</span>
        </span>
      </a-table>
    </div>

    <paishui-rule-modal ref="modalForm" @ok="modalFormOk"></paishui-rule-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import PaishuiRuleModal from '@views/modules/PaishuiRule/PaishuiRuleModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import {httpAction} from '@api/manage'

  export default {
    name: 'PaishuiRuleList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      PaishuiRuleModal
    },
    data () {
      return {
        description: '报警规则表管理页面',
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'规则名称',
            align:"center",
            dataIndex: 'ruleName'
          },
          {
            title:'规则描述',
            align:"center",
            dataIndex: 'ruleDesc'
          },
          {
            title:'生效区域',
            align:"center",
            dataIndex: 'areaId_dictText'
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            // customRender: (text, record, index) => text ? (text == 1 ? '启用' :  '停用' ) : ''
            scopedSlots: {customRender: 'ruleStatus'}
          },
          {
            title:'设置时间',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/paishui/paishuiRule/list",
          delete: "/paishui/paishuiRule/delete",
          deleteBatch: "/paishui/paishuiRule/deleteBatch",
          exportXlsUrl: "/paishui/paishuiRule/exportXls",
          importExcelUrl: "paishui/paishuiRule/importExcel",
          edit:'/paishui/paishuiRule/edit'
        },
        dictOptions:{},
        superFieldList:[],
        ruleList:true
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'int',value:'version',text:'版本号',dictCode:''})
        fieldList.push({type:'string',value:'ruleName',text:'规章名称',dictCode:''})
        fieldList.push({type:'string',value:'ruleDesc',text:'规章描述',dictCode:''})
        fieldList.push({type:'sel_depart',value:'areaId',text:'有效区域id'})
        fieldList.push({type:'int',value:'equType',text:'设备类型',dictCode:'ps_equ_type'})
        fieldList.push({type:'int',value:'level',text:'报警级别',dictCode:'ps_police_lev'})
        fieldList.push({type:'string',value:'contentField',text:'规则内容字段',dictCode:''})
        fieldList.push({type:'int',value:'contentOperator',text:'规则内容比较符',dictCode:'ps_operator'})
        fieldList.push({type:'BigDecimal',value:'contentLimit',text:'规则内容阈值',dictCode:''})
        fieldList.push({type:'string',value:'content',text:'报警字段',dictCode:''})
        fieldList.push({type:'int',value:'status',text:'状态 0生效 1失效',dictCode:''})
        fieldList.push({type:'int',value:'policeType',text:'报警类型',dictCode:'ps_police_type'})
        this.superFieldList = fieldList
      },
      selectorSwitch(record,checked,event){
        console.log('record record',record)
        console.log('checked checked',checked)
        console.log('event event',event)
        if (checked) record.status = 0
        else if (checked == false) record.status = 1
        httpAction(this.url.edit,record,'post').then(res => {
          console.log('res res',res)
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>